function [savings_EWM] = generate_tables_covariate(sample_cross,winter_prevuse,wave,cost,bsimax,SMC,savedir,fbase,rule,fullsample)

% This function calls two functions to produce results for the savings table: 
% 1) quadrant rule grid search algorithms; 
% 2) cubic rule cplex linear programming solver 
% The savings are for treatment rules in the two-dimensional space
% based on the selected covariate and baseline usage. Covariate choices are
% income, house size, and house age (vintage). 

% Inputs: 
% (1) sample_cross: cross-sectional dataset with electricity consumption, 
% covariates, and propensity scores
% (2) winter_prevuse: indicates whether baseline consumption is calculated 
% as the mean of consumption in winter months (Jan and Feb) or as the mean
% of specified pre-treatment periods
% (3) wave: indicates whether the output is wave-specific (=3,6,or 7) or
% for the pooled sample (=0)
% (4) cost: =1 use cost at outcome;  =0 use kWh as outcome
% (5) bsimax: number of bootstrap runs
% (6) SMC: =1 use social marginal cost; =0 use retail electricity price
% (7) savedir: directory to save the savings table 
% (8) fbase: string used to indicate the propensity score and baseline
% months specification for output table 
% (9) rule: quadrant or cubic;
% (10) fullsample: wheather full sample or estimation sample

% Output: 
% (1) savings_EWM: table summarizing savings from quadrant or cubic rules 

%% Specify covariates of interest
covariates={'income' 'size' 'vintage'};

%% Specify the private marginal cost used to calculate cost savings cost 
if (cost)
    tcost = 0.765; % cost of sending out letter for each household. see footnote in paper for detailed calculation.
else
    tcost = 0;
end

%%
if rule=="quadrant"
%% Calculate EWM quadrant savings
    savings_EWM_quadrant_income  = generate_results_quadrant_rule(bsimax,sample_cross,wave,tcost,covariates{1},winter_prevuse,SMC,fullsample,savedir,fbase);
    savings_EWM_quadrant_size    = generate_results_quadrant_rule(bsimax,sample_cross,wave,tcost,covariates{2},winter_prevuse,SMC,fullsample,savedir,fbase);
    savings_EWM_quadrant_vintage = generate_results_quadrant_rule(bsimax,sample_cross,wave,tcost,covariates{3},winter_prevuse,SMC,fullsample,savedir,fbase);
    savings_EWM = [savings_EWM_quadrant_income; savings_EWM_quadrant_size; savings_EWM_quadrant_vintage];
elseif rule=="cubic"
%% Calculate EWM cubic savings 
    savings_EWM_cubic_income  = generate_results_cubic_rule(bsimax,sample_cross,wave,tcost,covariates{1},winter_prevuse,SMC,fullsample,savedir,fbase);
    savings_EWM_cubic_size    = generate_results_cubic_rule(bsimax,sample_cross,wave,tcost,covariates{2},winter_prevuse,SMC,fullsample,savedir,fbase);
    savings_EWM_cubic_vintage = generate_results_cubic_rule(bsimax,sample_cross,wave,tcost,covariates{3},winter_prevuse,SMC,fullsample,savedir,fbase);
    savings_EWM = [savings_EWM_cubic_income; savings_EWM_cubic_size; savings_EWM_cubic_vintage];
end

end